Page({
  data:{
    appId:'',
    appName:'',
    pageTitle:'',
    pageDesc:'',
    sections:[],
    formData:{},
    fieldMeta:{},
    approverOptions:[]
  },
  onLoad(options){
    const appId=options.appId||options.app_id||''
    const appName=decodeURIComponent(options.appName||options.app_name||'')
    this.setData({appId,appName})
    this.initPage()
  },
  initPage(){
    const title=this.data.appName?`发起申请：${this.data.appName}`:'发起申请'
    const desc='请填写必要信息后提交'
    let sections=this.buildSchemaForApp(this.data.appId,this.data.appName)
    sections=[...sections,
      {id:'approval',name:'审批配置',fields:[
        {id:'approver',label:'审核人',type:'select',placeholder:'请选择',options:[]}
      ]}
    ]
    const formData={}
    const fieldMeta={}
    sections.forEach(s=>{
      if(s.type==='repeater'){
        formData[s.id]=s.default||[{}]
        ;(s.fields||[]).forEach(f=>{if(f.type==='select'&&Array.isArray(f.options)){f.optionsLabels=(f.options||[]).map(o=>o.label||o.name||String(o))}})
      }else{
        (s.fields||[]).forEach(f=>{
          if(f.type==='select'&&Array.isArray(f.options)){f.optionsLabels=(f.options||[]).map(o=>o.label||o.name||String(o))}
          if(f.type==='uploader'){formData[f.id]=[]}
          else{formData[f.id]=f.default||''}
          fieldMeta[f.id]=f
        })
      }
    })
    this.setData({pageTitle:title,pageDesc:desc,sections,formData,fieldMeta})
    this.loadApproverOptions()
  },
  buildSchemaForApp(appId,appName){
    const typeMap={
      '27':'vehicle',
      '28':'meeting',
      '29':'budget',
      '30':'staff_need',
      '31':'leave',
      '32':'resign',
      '33':'contract_apply',
      '34':'seal_borrow',
      '36':'purchase',
      '37':'issue',
      '38':'travel',
      '39':'reimburse',
      '40':'payment_request',
      '41':'loan_request',
      '42':'agency_payment',
      '4':'finance'
    }
    const type=typeMap[String(appId)]||''
    if(type==='leave'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'applyDate',label:'申请日期',type:'date',placeholder:'请选择日期'},
          {id:'department',label:'申请部门',type:'input',placeholder:'请输入部门'},
          {id:'position',label:'职务',type:'input',placeholder:'请输入职务'},
          {id:'leaveType',label:'假期类型',type:'select',placeholder:'请选择',options:[{label:'年假',value:'annual'},{label:'事假',value:'personal'},{label:'病假',value:'sick'}]},
          {id:'leaveReason',label:'请假事由',type:'textarea',placeholder:'请填写事由'}
        ]},
        {id:'details',name:'请假明细',type:'repeater',fields:[
          {id:'startDate',label:'开始日期',type:'date',placeholder:'请选择日期'},
          {id:'endDate',label:'结束日期',type:'date',placeholder:'请选择日期'},
          {id:'timeSlot',label:'时间段',type:'select',placeholder:'请选择',options:[{label:'全天',value:'all'},{label:'上午',value:'am'},{label:'下午',value:'pm'}]}
        ],default:[{}]},
        {id:'summary',name:'其他信息',fields:[
          {id:'totalDays',label:'合计工作日',type:'input',placeholder:'请输入天数'},
          {id:'contact',label:'联系方式',type:'input',placeholder:'请输入联系方式'},
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='finance'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'amount',label:'金额',type:'input',placeholder:'请输入金额'},
          {id:'customer',label:'客户',type:'input',placeholder:'请输入客户名称'}
        ]},
        {id:'invoice',name:'票据',fields:[
          {id:'invoiceDate',label:'开票日期',type:'date',placeholder:'请选择日期'},
          {id:'invoiceNo',label:'发票号',type:'input',placeholder:'请输入发票号'}
        ]},
        {id:'remark',name:'备注',fields:[
          {id:'remark',label:'备注',type:'textarea',placeholder:'请填写备注说明'},
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='vehicle'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'number',label:'编号',type:'input',placeholder:'请输入编号'},
          {id:'applicant',label:'申请人',type:'input',placeholder:'请输入申请人'},
          {id:'department',label:'所属部门',type:'input',placeholder:'请输入部门'}
        ]},
        {id:'vehicle',name:'车辆信息',fields:[
          {id:'plate',label:'车牌号',type:'input',placeholder:'请输入车牌号'},
          {id:'property',label:'车辆性质',type:'select',placeholder:'请选择',options:[{label:'公司车辆',value:'company'},{label:'个人车辆',value:'personal'}]},
          {id:'reportType',label:'报备类型',type:'select',placeholder:'请选择',options:[{label:'外出',value:'out'},{label:'外地',value:'outcity'},{label:'长期停放',value:'park'}]}
        ]},
        {id:'remark',name:'备注',fields:[
          {id:'remark',label:'备注',type:'textarea',placeholder:'请填写备注说明'}
        ]}
      ]
    }
    if(type==='meeting'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'subject',label:'会议主题',type:'input',placeholder:'请输入会议主题'},
          {id:'location',label:'会议地点',type:'input',placeholder:'请输入会议地点'},
          {id:'convener',label:'召集人',type:'input',placeholder:'请输入召集人'},
          {id:'attendees',label:'参会人员',type:'textarea',placeholder:'请输入参会人员'}
        ]},
        {id:'time',name:'时间',fields:[
          {id:'startDate',label:'开始日期',type:'date',placeholder:'请选择日期'},
          {id:'startTime',label:'开始时间',type:'time',placeholder:'请选择时间'},
          {id:'endDate',label:'结束日期',type:'date',placeholder:'请选择日期'},
          {id:'endTime',label:'结束时间',type:'time',placeholder:'请选择时间'}
        ]},
        {id:'agenda',name:'议程列表',type:'repeater',fields:[
          {id:'topic',label:'议题',type:'input',placeholder:'请输入议题'},
          {id:'reporter',label:'汇报人',type:'input',placeholder:'请输入汇报人'},
          {id:'attachment',label:'附件名称',type:'input',placeholder:'请输入附件'}
        ],default:[{}]}
      ]
    }
    if(type==='budget'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'department',label:'申请部门',type:'input',placeholder:'请输入部门'},
          {id:'budgetMonth',label:'费用月份',type:'date',placeholder:'请选择日期'},
          {id:'budgetCategory',label:'预算类别',type:'select',placeholder:'请选择',options:[{label:'办公',value:'office'},{label:'差旅',value:'travel'},{label:'采购',value:'purchase'}]},
          {id:'amount',label:'金额',type:'input',placeholder:'请输入金额'}
        ]},
        {id:'reason',name:'事由与附件',fields:[
          {id:'reason',label:'申请事由',type:'textarea',placeholder:'请填写事由'},
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='staff_need'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'department',label:'部门',type:'input',placeholder:'请输入部门'},
          {id:'position',label:'岗位',type:'input',placeholder:'请输入岗位'},
          {id:'headcount',label:'人数',type:'input',placeholder:'请输入人数'},
          {id:'needDate',label:'到岗日期',type:'date',placeholder:'请选择日期'}
        ]},
        {id:'reason',name:'需求说明',fields:[
          {id:'reason',label:'说明',type:'textarea',placeholder:'请填写说明'},
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='resign'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'department',label:'申请部门',type:'input',placeholder:'请输入部门'},
          {id:'position',label:'职位',type:'input',placeholder:'请输入职位'},
          {id:'resignDate',label:'本人于',type:'date',placeholder:'请选择日期'}
        ]},
        {id:'reason',name:'原因与附件',fields:[
          {id:'resignReason',label:'辞职理由',type:'textarea',placeholder:'请填写理由'},
          {id:'files',label:'附件上传',type:'uploader'}
        ]},
        {id:'follow',name:'后续信息',fields:[
          {id:'lastWorkDate',label:'最后工作日期',type:'date',placeholder:'请选择日期'},
          {id:'leaderSign',label:'领导会签',type:'input',placeholder:'请选择会签人员'}
        ]}
      ]
    }
    if(type==='contract_apply'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'contractTitle',label:'合同标题',type:'input',placeholder:'请输入合同标题'},
          {id:'counterparty',label:'相对方',type:'input',placeholder:'请输入相对方'},
          {id:'amount',label:'合同金额',type:'input',placeholder:'请输入金额'},
          {id:'startDate',label:'开始日期',type:'date',placeholder:'请选择日期'},
          {id:'endDate',label:'结束日期',type:'date',placeholder:'请选择日期'}
        ]},
        {id:'files',name:'附件',fields:[
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='seal_borrow'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'department',label:'部门',type:'input',placeholder:'请输入部门'},
          {id:'sealType',label:'印章类型',type:'select',placeholder:'请选择',options:[{label:'公章',value:'company'},{label:'合同章',value:'contract'},{label:'财务章',value:'finance'}]}
        ]},
        {id:'use',name:'使用信息',fields:[
          {id:'startDate',label:'开始日期',type:'date',placeholder:'请选择日期'},
          {id:'startTime',label:'开始时间',type:'time',placeholder:'请选择时间'},
          {id:'endDate',label:'结束日期',type:'date',placeholder:'请选择日期'},
          {id:'endTime',label:'结束时间',type:'time',placeholder:'请选择时间'},
          {id:'reason',label:'使用事由',type:'textarea',placeholder:'请填写事由'}
        ]}
      ]
    }
    if(type==='purchase'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'department',label:'申请部门',type:'input',placeholder:'请输入部门'},
          {id:'supplier',label:'供应商',type:'input',placeholder:'请输入供应商'},
          {id:'arriveDate',label:'到货日期',type:'date',placeholder:'请选择日期'}
        ]},
        {id:'items',name:'采购明细',type:'repeater',fields:[
          {id:'itemName',label:'物资名称',type:'input',placeholder:'请输入名称'},
          {id:'quantity',label:'数量',type:'input',placeholder:'请输入数量'},
          {id:'unitPrice',label:'单价',type:'input',placeholder:'请输入单价'},
          {id:'total',label:'金额',type:'input',placeholder:'请输入金额'}
        ],default:[{}]},
        {id:'files',name:'附件',fields:[
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='issue'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'department',label:'领用部门',type:'input',placeholder:'请输入部门'},
          {id:'receiver',label:'领用人',type:'input',placeholder:'请输入领用人'},
          {id:'issueDate',label:'领用日期',type:'date',placeholder:'请选择日期'}
        ]},
        {id:'items',name:'领用明细',type:'repeater',fields:[
          {id:'itemName',label:'物资名称',type:'input',placeholder:'请输入名称'},
          {id:'quantity',label:'数量',type:'input',placeholder:'请输入数量'}
        ],default:[{}]},
        {id:'reason',name:'备注',fields:[
          {id:'remark',label:'备注',type:'textarea',placeholder:'请填写备注'}
        ]}
      ]
    }
    if(type==='travel'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'applicant',label:'申请人',type:'input',placeholder:'请输入申请人'},
          {id:'applyDate',label:'申请日期',type:'date',placeholder:'请选择日期'},
          {id:'department',label:'申请部门',type:'input',placeholder:'请输入部门'},
          {id:'payCompany',label:'付费公司',type:'select',placeholder:'请选择',options:[{label:'公司A',value:'A'},{label:'公司B',value:'B'}]},
          {id:'traveler',label:'出差人',type:'input',placeholder:'请输入出差人'},
          {id:'reason',label:'出差事由',type:'textarea',placeholder:'请填写事由'}
        ]},
        {id:'expense',name:'费用与预算',fields:[
          {id:'expenseType',label:'支出类别',type:'select',placeholder:'请选择',options:[{label:'交通',value:'transport'},{label:'住宿',value:'lodging'},{label:'其他',value:'other'}]},
          {id:'expenseItem',label:'支出项目',type:'input',placeholder:'请输入项目'},
          {id:'amount',label:'报销总额',type:'input',placeholder:'请输入金额'},
          {id:'budgetInfo',label:'预算信息',type:'input',placeholder:'请输入预算信息'}
        ]},
        {id:'trip',name:'行程明细',type:'repeater',fields:[
          {id:'departDate',label:'出发日期',type:'date',placeholder:'请选择日期'},
          {id:'departTime',label:'出发时间',type:'time',placeholder:'请选择时间'},
          {id:'departPlace',label:'出发地点',type:'input',placeholder:'请输入地点'},
          {id:'arriveDate',label:'到达日期',type:'date',placeholder:'请选择日期'},
          {id:'arriveTime',label:'到达时间',type:'time',placeholder:'请选择时间'},
          {id:'arrivePlace',label:'到达地点',type:'input',placeholder:'请输入地点'},
          {id:'transport',label:'交通工具',type:'select',placeholder:'请选择',options:[{label:'火车',value:'train'},{label:'飞机',value:'air'},{label:'汽车',value:'car'}]},
          {id:'transportAmount',label:'交通金额',type:'input',placeholder:'请输入金额'},
          {id:'lodgingDays',label:'住宿天数',type:'input',placeholder:'请输入天数'},
          {id:'lodgingAmount',label:'住宿金额',type:'input',placeholder:'请输入金额'},
          {id:'otherItem',label:'其他项目',type:'input',placeholder:'请输入项目'},
          {id:'otherAmount',label:'其他金额',type:'input',placeholder:'请输入金额'}
        ],default:[{}]},
        {id:'files',name:'附件',fields:[
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='reimburse'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'department',label:'部门',type:'input',placeholder:'请输入部门'}
        ]},
        {id:'items',name:'报销明细',type:'repeater',fields:[
          {id:'expenseType',label:'费用类型',type:'select',placeholder:'请选择',options:[{label:'交通',value:'transport'},{label:'餐饮',value:'meal'},{label:'住宿',value:'lodging'},{label:'其他',value:'other'}]},
          {id:'amount',label:'金额',type:'input',placeholder:'请输入金额'},
          {id:'date',label:'日期',type:'date',placeholder:'请选择日期'},
          {id:'remark',label:'备注',type:'input',placeholder:'请输入备注'}
        ],default:[{}]},
        {id:'files',name:'附件',fields:[
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='payment_request'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'project',label:'关联项目',type:'input',placeholder:'请输入项目'},
          {id:'contractNo',label:'合同编号',type:'input',placeholder:'请输入合同编号'},
          {id:'amount',label:'请款金额',type:'input',placeholder:'请输入金额'}
        ]},
        {id:'payee',name:'收款信息',fields:[
          {id:'payee',label:'收款单位',type:'input',placeholder:'请输入单位'},
          {id:'bankAccount',label:'账号',type:'input',placeholder:'请输入账号'},
          {id:'bankName',label:'开户行',type:'input',placeholder:'请输入开户行'}
        ]},
        {id:'files',name:'附件',fields:[
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='loan_request'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'amount',label:'借款金额',type:'input',placeholder:'请输入金额'},
          {id:'purpose',label:'借款用途',type:'input',placeholder:'请输入用途'},
          {id:'returnDate',label:'归还日期',type:'date',placeholder:'请选择日期'}
        ]},
        {id:'payee',name:'收款信息',fields:[
          {id:'payee',label:'收款人',type:'input',placeholder:'请输入收款人'},
          {id:'bankAccount',label:'账号',type:'input',placeholder:'请输入账号'},
          {id:'bankName',label:'开户行',type:'input',placeholder:'请输入开户行'}
        ]},
        {id:'files',name:'附件',fields:[
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(type==='agency_payment'){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'category',label:'缴款类别',type:'select',placeholder:'请选择',options:[{label:'契税',value:'deed'},{label:'印花税',value:'stamp'},{label:'其他',value:'other'}]},
          {id:'amount',label:'缴款金额',type:'input',placeholder:'请输入金额'},
          {id:'customer',label:'客户',type:'input',placeholder:'请输入客户'},
          {id:'project',label:'项目',type:'input',placeholder:'请输入项目'}
        ]},
        {id:'files',name:'附件',fields:[
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    const name=appName||''
    if(name.indexOf('车辆')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'number',label:'编号',type:'input',placeholder:'请输入编号'},
          {id:'applicant',label:'申请人',type:'input',placeholder:'请输入申请人'},
          {id:'department',label:'所属部门',type:'input',placeholder:'请输入部门'}
        ]},
        {id:'vehicle',name:'车辆信息',fields:[
          {id:'plate',label:'车牌号',type:'input',placeholder:'请输入车牌号'},
          {id:'property',label:'车辆性质',type:'select',placeholder:'请选择',options:[{label:'公司车辆',value:'company'},{label:'个人车辆',value:'personal'}]},
          {id:'reportType',label:'报备类型',type:'select',placeholder:'请选择',options:[{label:'外出',value:'out'},{label:'外地',value:'outcity'},{label:'长期停放',value:'park'}]}
        ]},
        {id:'remark',name:'备注',fields:[
          {id:'remark',label:'备注',type:'textarea',placeholder:'请填写备注说明'}
        ]}
      ]
    }
    if(name.indexOf('假期')>-1||name.indexOf('员工假期')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'applyDate',label:'申请日期',type:'date',placeholder:'请选择日期'},
          {id:'department',label:'申请部门',type:'input',placeholder:'请输入部门'},
          {id:'position',label:'职务',type:'input',placeholder:'请输入职务'},
          {id:'leaveType',label:'假期类型',type:'select',placeholder:'请选择',options:[{label:'年假',value:'annual'},{label:'事假',value:'personal'},{label:'病假',value:'sick'}]},
          {id:'leaveReason',label:'请假事由',type:'textarea',placeholder:'请填写事由'}
        ]},
        {id:'details',name:'请假明细',type:'repeater',fields:[
          {id:'startDate',label:'开始日期',type:'date',placeholder:'请选择日期'},
          {id:'endDate',label:'结束日期',type:'date',placeholder:'请选择日期'},
          {id:'timeSlot',label:'时间段',type:'select',placeholder:'请选择',options:[{label:'全天',value:'all'},{label:'上午',value:'am'},{label:'下午',value:'pm'}]}
        ],default:[{}]},
        {id:'summary',name:'其他信息',fields:[
          {id:'totalDays',label:'合计工作日',type:'input',placeholder:'请输入天数'},
          {id:'contact',label:'联系方式',type:'input',placeholder:'请输入联系方式'},
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    if(name.indexOf('辞职')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'department',label:'申请部门',type:'input',placeholder:'请输入部门'},
          {id:'position',label:'职位',type:'input',placeholder:'请输入职位'},
          {id:'resignDate',label:'本人于',type:'date',placeholder:'请选择日期'}
        ]},
        {id:'reason',name:'原因与附件',fields:[
          {id:'resignReason',label:'辞职理由',type:'textarea',placeholder:'请填写理由'},
          {id:'files',label:'附件上传',type:'uploader'}
        ]},
        {id:'follow',name:'后续信息',fields:[
          {id:'lastWorkDate',label:'最后工作日期',type:'date',placeholder:'请选择日期'},
          {id:'leaderSign',label:'领导会签',type:'input',placeholder:'请选择会签人员'}
        ]}
      ]
    }
    if(name.indexOf('差旅')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'applicant',label:'申请人',type:'input',placeholder:'请输入申请人'},
          {id:'applyDate',label:'申请日期',type:'date',placeholder:'请选择日期'},
          {id:'department',label:'申请部门',type:'input',placeholder:'请输入部门'},
          {id:'payCompany',label:'付费公司',type:'select',placeholder:'请选择',options:[{label:'公司A',value:'A'},{label:'公司B',value:'B'}]},
          {id:'traveler',label:'出差人',type:'input',placeholder:'请输入出差人'},
          {id:'reason',label:'出差事由',type:'textarea',placeholder:'请填写事由'}
        ]},
        {id:'expense',name:'费用与预算',fields:[
          {id:'expenseType',label:'支出类别',type:'select',placeholder:'请选择',options:[{label:'交通',value:'transport'},{label:'住宿',value:'lodging'},{label:'其他',value:'other'}]},
          {id:'expenseItem',label:'支出项目',type:'input',placeholder:'请输入项目'},
          {id:'amount',label:'报销总额',type:'input',placeholder:'请输入金额'},
          {id:'budgetInfo',label:'预算信息',type:'input',placeholder:'请输入预算信息'}
        ]},
        {id:'trip',name:'行程明细',type:'repeater',fields:[
          {id:'departDate',label:'出发日期',type:'date',placeholder:'请选择日期'},
          {id:'departTime',label:'出发时间',type:'time',placeholder:'请选择时间'},
          {id:'departPlace',label:'出发地点',type:'input',placeholder:'请输入地点'},
          {id:'arriveDate',label:'到达日期',type:'date',placeholder:'请选择日期'},
          {id:'arriveTime',label:'到达时间',type:'time',placeholder:'请选择时间'},
          {id:'arrivePlace',label:'到达地点',type:'input',placeholder:'请输入地点'},
          {id:'transport',label:'交通工具',type:'select',placeholder:'请选择',options:[{label:'火车',value:'train'},{label:'飞机',value:'air'},{label:'汽车',value:'car'}]},
          {id:'transportAmount',label:'交通金额',type:'input',placeholder:'请输入金额'},
          {id:'lodgingDays',label:'住宿天数',type:'input',placeholder:'请输入天数'},
          {id:'lodgingAmount',label:'住宿金额',type:'input',placeholder:'请输入金额'},
          {id:'otherItem',label:'其他项目',type:'input',placeholder:'请输入项目'},
          {id:'otherAmount',label:'其他金额',type:'input',placeholder:'请输入金额'}
        ],default:[{}]},
        {id:'files',name:'附件',fields:[
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    const admin=['行政人事','员工假期','合同管理','工单管理']
    const finance=['财务管理']
    if(admin.includes(name)){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'subject',label:'主题',type:'input',placeholder:'请输入主题'},
          {id:'department',label:'部门',type:'input',placeholder:'请输入部门'},
          {id:'location',label:'地点',type:'input',placeholder:'请输入地点'}
        ]},
        {id:'time',name:'时间',fields:[
          {id:'startDate',label:'开始日期',type:'date',placeholder:'请选择日期'},
          {id:'startTime',label:'开始时间',type:'time',placeholder:'请选择时间'},
          {id:'endDate',label:'结束日期',type:'date',placeholder:'请选择日期'},
          {id:'endTime',label:'结束时间',type:'time',placeholder:'请选择时间'}
        ]},
        {id:'people',name:'人员',fields:[
          {id:'attendees',label:'参会人员',type:'textarea',placeholder:'请输入人员名单'},
          {id:'convener',label:'召集人',type:'input',placeholder:'请输入召集人'},
          {id:'reporter',label:'汇报人',type:'input',placeholder:'请输入汇报人'}
        ]}
      ]
    }
    if(name.indexOf('商机')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'name',label:'商机名称',type:'input',placeholder:'请输入商机名称'},
          {id:'number',label:'商机编号',type:'input',placeholder:'请输入商机编号'},
          {id:'customer_id',label:'客户',type:'input',placeholder:'请输入客户'},
          {id:'chance_source',label:'商机来源',type:'input',placeholder:'请输入商机来源'},
          {id:'sale_id',label:'销售负责人',type:'input',placeholder:'请输入销售负责人'},
          {id:'dept_id',label:'负责人部门',type:'input',placeholder:'请输入部门'}
        ]},
        {id:'forecast',name:'预测信息',fields:[
          {id:'signup_date',label:'预计签约日期',type:'date',placeholder:'请选择日期'},
          {id:'signup_money',label:'预计签单金额',type:'input',placeholder:'请输入金额'},
          {id:'signup_probability',label:'成单率(%)',type:'input',placeholder:'请输入成单率'}
        ]},
        {id:'other',name:'其他信息',fields:[
          {id:'address',label:'地址',type:'input',placeholder:'请输入地址'},
          {id:'remark',label:'描述',type:'textarea',placeholder:'请输入描述'}
        ]}
      ]
    }
    if(name.indexOf('估价项目PM')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'name',label:'项目名称',type:'input',placeholder:'请输入项目名称'},
          {id:'report_number',label:'报告编号',type:'input',placeholder:'请输入报告编号'},
          {id:'code_head',label:'编号简称',type:'input',placeholder:'请输入编号简称'},
          {id:'type',label:'项目类型',type:'input',placeholder:'请输入项目类型'},
          {id:'dept_id',label:'发起部门',type:'input',placeholder:'请输入部门'}
        ]},
        {id:'detail',name:'项目详情',fields:[
          {id:'address',label:'坐落',type:'input',placeholder:'请输入坐落'},
          {id:'area',label:'所属区域',type:'input',placeholder:'请输入所属区域'},
          {id:'appraisal_purpose',label:'估价目的',type:'input',placeholder:'请输入估价目的'},
          {id:'estimated_money',label:'预计收费金额',type:'input',placeholder:'请输入金额'}
        ]},
        {id:'party',name:'相关方',fields:[
          {id:'principal',label:'委托方',type:'input',placeholder:'请输入委托方'},
          {id:'principal_user',label:'委托方联系人',type:'input',placeholder:'请输入联系人'},
          {id:'principal_phone',label:'联系电话',type:'input',placeholder:'请输入电话'}
        ]}
      ]
    }
    if(name.indexOf('估价项目')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'name',label:'项目名称',type:'input',placeholder:'请输入项目名称'},
          {id:'report_number',label:'报告编号',type:'input',placeholder:'请输入报告编号'},
          {id:'code_head',label:'编号简称',type:'input',placeholder:'请输入编号简称'},
          {id:'type',label:'项目类型',type:'input',placeholder:'请输入项目类型'}
        ]},
        {id:'detail',name:'项目详情',fields:[
          {id:'address',label:'坐落',type:'input',placeholder:'请输入坐落'},
          {id:'area',label:'所属区域',type:'input',placeholder:'请输入所属区域'},
          {id:'appraisal_purpose',label:'估价目的',type:'input',placeholder:'请输入估价目的'},
          {id:'estimated_money',label:'预计收费金额',type:'input',placeholder:'请输入金额'}
        ]},
        {id:'party',name:'相关方',fields:[
          {id:'principal',label:'委托方',type:'input',placeholder:'请输入委托方'},
          {id:'principal_user',label:'委托方联系人',type:'input',placeholder:'请输入联系人'},
          {id:'principal_phone',label:'联系电话',type:'input',placeholder:'请输入电话'}
        ]}
      ]
    }
    if(name.indexOf('招投标')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'project_name',label:'项目名称',type:'input',placeholder:'请输入项目名称'},
          {id:'code',label:'招投标编号',type:'input',placeholder:'请输入编号'},
          {id:'bidding_type',label:'招投标方式',type:'input',placeholder:'请输入方式'},
          {id:'dept_id',label:'发起部门',type:'input',placeholder:'请输入部门'}
        ]},
        {id:'agency',name:'代理与委托',fields:[
          {id:'principal',label:'委托方',type:'input',placeholder:'请输入委托方'},
          {id:'agency',label:'代理机构',type:'input',placeholder:'请输入代理机构'},
          {id:'agency_user',label:'代理联系人',type:'input',placeholder:'请输入联系人'}
        ]},
        {id:'time_money',name:'时间与金额',fields:[
          {id:'bid_open_time',label:'开标时间',type:'date',placeholder:'请选择时间'},
          {id:'max_limited_price',label:'最高限价',type:'input',placeholder:'请输入金额'},
          {id:'bid_quotation',label:'投标报价',type:'input',placeholder:'请输入报价'}
        ]}
      ]
    }
    if(name.indexOf('客户')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'customer_name',label:'客户名称',type:'input',placeholder:'请输入客户名称'},
          {id:'customer_level',label:'客户等级',type:'input',placeholder:'请输入等级'},
          {id:'customer_classification',label:'客户分类',type:'input',placeholder:'请输入分类'},
          {id:'status',label:'状态',type:'input',placeholder:'请输入状态'}
        ]},
        {id:'finance',name:'财务信息',fields:[
          {id:'invoice_title',label:'发票抬头',type:'input',placeholder:'请输入发票抬头'},
          {id:'ident_number',label:'纳税人识别号',type:'input',placeholder:'请输入识别号'},
          {id:'deposit_bank',label:'开户银行',type:'input',placeholder:'请输入开户行'},
          {id:'bank_account',label:'银行账号',type:'input',placeholder:'请输入账号'}
        ]},
        {id:'contact',name:'联系信息',fields:[
          {id:'contact_address',label:'联系地址',type:'input',placeholder:'请输入地址'},
          {id:'contact_phone',label:'联系电话',type:'input',placeholder:'请输入电话'},
          {id:'remark',label:'备注',type:'textarea',placeholder:'请输入备注'}
        ]}
      ]
    }
    if(name.indexOf('资产项目')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'report_name',label:'报告名称',type:'input',placeholder:'请输入报告名称'},
          {id:'report_number',label:'报告文号',type:'input',placeholder:'请输入报告文号'},
          {id:'type',label:'项目类型',type:'input',placeholder:'请输入项目类型'},
          {id:'dept_id',label:'发起部门',type:'input',placeholder:'请输入部门'}
        ]},
        {id:'client',name:'委托与被评估',fields:[
          {id:'client_name',label:'委托人名称',type:'input',placeholder:'请输入委托人'},
          {id:'assessed_name',label:'被评估单位',type:'input',placeholder:'请输入被评估单位'},
          {id:'assess_purpose',label:'评估目的',type:'input',placeholder:'请输入评估目的'}
        ]},
        {id:'assess',name:'评估详情',fields:[
          {id:'assess_date',label:'评估基准日',type:'date',placeholder:'请选择日期'},
          {id:'asset_service_money',label:'服务费',type:'input',placeholder:'请输入金额'},
          {id:'assess_method',label:'评估方法',type:'input',placeholder:'请输入方法'}
        ]}
      ]
    }
    if(name.indexOf('勘测')>-1){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'name',label:'项目名称',type:'input',placeholder:'请输入项目名称'},
          {id:'code',label:'项目编号',type:'input',placeholder:'请输入项目编号'},
          {id:'dept_id',label:'发起部门',type:'input',placeholder:'请输入部门'},
          {id:'city',label:'所属城市',type:'input',placeholder:'请输入城市'}
        ]},
        {id:'party',name:'相关方',fields:[
          {id:'principal',label:'委托方',type:'input',placeholder:'请输入委托方'},
          {id:'principal_user',label:'委托方联系人',type:'input',placeholder:'请输入联系人'},
          {id:'principal_phone',label:'联系电话',type:'input',placeholder:'请输入电话'}
        ]},
        {id:'other',name:'其他信息',fields:[
          {id:'money',label:'项目金额',type:'input',placeholder:'请输入金额'},
          {id:'remark',label:'备注',type:'textarea',placeholder:'请输入备注'}
        ]}
      ]
    }
    if(finance.includes(name)){
      return [
        {id:'basic',name:'基本信息',fields:[
          {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
          {id:'amount',label:'金额',type:'input',placeholder:'请输入金额'},
          {id:'customer',label:'客户',type:'input',placeholder:'请输入客户名称'}
        ]},
        {id:'invoice',name:'票据',fields:[
          {id:'invoiceDate',label:'开票日期',type:'date',placeholder:'请选择日期'},
          {id:'invoiceNo',label:'发票号',type:'input',placeholder:'请输入发票号'}
        ]},
        {id:'remark',name:'备注',fields:[
          {id:'remark',label:'备注',type:'textarea',placeholder:'请填写备注说明'},
          {id:'files',label:'附件上传',type:'uploader'}
        ]}
      ]
    }
    return [
      {id:'basic',name:'基本信息',fields:[
        {id:'title',label:'标题',type:'input',placeholder:'请输入标题'},
        {id:'subject',label:'主题',type:'input',placeholder:'请输入主题'}
      ]},
      {id:'remark',name:'备注',fields:[
        {id:'remark',label:'备注',type:'textarea',placeholder:'请填写备注说明'}
      ]}
    ]
  },
  loadApproverOptions(){
    let deptId=null
    try{
      const user=wx.getStorageSync('user_info')||{}
      deptId=user.part_id_id||user.part_id||user.department_id||null
    }catch(e){}
    const url=`${getApp().globalData.serverUrl}/GetDepartmentUsers/`
    const fallback=[{label:'庄嘉豪',value:1},{label:'李四',value:2},{label:'王五',value:3}]
    wx.request({
      url,
      method:'POST',
      data: deptId? {department_id:deptId}:{},
      success:(res)=>{
        let users=[]
        try{
          const arr=res.data.users||res.data.data||[]
          users=arr.map(u=>({label:u.zh_name||u.name||u.username, value:u.id||u.user_id}))
        }catch(e){users=[]}
        const options=users.length?users:fallback
        const labels=options.map(o=>o.label)
        const field=this.data.fieldMeta['approver']||{}
        field.options=options
        field.optionsLabels=labels
        const obj={}
        obj['fieldMeta.approver']=field
        this.setData(obj)
      },
      fail:()=>{
        const options=fallback
        const labels=options.map(o=>o.label)
        const field=this.data.fieldMeta['approver']||{}
        field.options=options
        field.optionsLabels=labels
        const obj={}
        obj['fieldMeta.approver']=field
        this.setData(obj)
      }
    })
  },
  onFieldInput(e){
    const id=e.currentTarget.dataset.id
    const value=e.detail.value
    this.setData({[`formData.${id}`]:value})
  },
  onSelectChange(e){
    const id=e.currentTarget.dataset.id
    const idx=e.detail.value
    const meta=this.data.fieldMeta[id]||{}
    const opt=(meta.options||[])[idx]||{}
    const value=opt.value!==undefined?opt.value:opt.label
    const label=opt.label||String(value||'')
    const obj={}
    obj[`formData.${id}`]=value
    obj[`formData.${id}_label`]=label
    obj[`formData.${id}_idx`]=idx
    this.setData(obj)
  },
  onDateChange(e){
    const id=e.currentTarget.dataset.id
    const value=e.detail.value
    this.setData({[`formData.${id}`]:value})
  },
  onTimeChange(e){
    const id=e.currentTarget.dataset.id
    const value=e.detail.value
    this.setData({[`formData.${id}`]:value})
  },
  onRowFieldInput(e){
    const section=e.currentTarget.dataset.section
    const row=e.currentTarget.dataset.row
    const id=e.currentTarget.dataset.id
    const value=e.detail.value
    const key=`formData.${section}[${row}].${id}`
    this.setData({[key]:value})
  },
  onRowDateChange(e){
    const section=e.currentTarget.dataset.section
    const row=e.currentTarget.dataset.row
    const id=e.currentTarget.dataset.id
    const value=e.detail.value
    const key=`formData.${section}[${row}].${id}`
    this.setData({[key]:value})
  },
  onRowTimeChange(e){
    const section=e.currentTarget.dataset.section
    const row=e.currentTarget.dataset.row
    const id=e.currentTarget.dataset.id
    const value=e.detail.value
    const key=`formData.${section}[${row}].${id}`
    this.setData({[key]:value})
  },
  onRowSelectChange(e){
    const section=e.currentTarget.dataset.section
    const row=e.currentTarget.dataset.row
    const id=e.currentTarget.dataset.id
    const idx=e.detail.value
    const sec=this.data.sections.find(s=>s.id===section)||{}
    const meta=(sec.fields||[]).find(f=>f.id===id)||{}
    const opt=(meta.options||[])[idx]||{}
    const value=opt.value!==undefined?opt.value:opt.label
    const label=opt.label||String(value||'')
    const base=`formData.${section}[${row}]`
    const obj={}
    obj[`${base}.${id}`]=value
    obj[`${base}.${id}_label`]=label
    obj[`${base}.${id}_idx`]=idx
    this.setData(obj)
  },
  onAddRow(e){
    const section=e.currentTarget.dataset.section
    const list=this.data.formData[section]||[]
    list.push({})
    this.setData({[`formData.${section}`]:list})
  },
  onRemoveRow(e){
    const section=e.currentTarget.dataset.section
    const row=e.currentTarget.dataset.row
    const list=this.data.formData[section]||[]
    if(list.length>1){
      list.splice(row,1)
      this.setData({[`formData.${section}`]:list})
    }
  },
  onChooseFiles(e){
    const id=e.currentTarget.dataset.id
    wx.chooseMessageFile({count:5,type:'file',success:(res)=>{
      const files=(this.data.formData[id]||[]).concat(res.tempFiles||[])
      this.setData({[`formData.${id}`]:files})
    }})
  },
  onSave(){
    wx.showToast({title:'草稿已保存',icon:'success'})
  },
  onSubmit(){
    if(!this.data.formData.approver){
      wx.showToast({title:'请选择审核人',icon:'none'})
      return
    }
    const data={appId:this.data.appId,appName:this.data.appName,form:this.data.formData}
    wx.showLoading({title:'提交中...'})
    setTimeout(()=>{wx.hideLoading();wx.showToast({title:'提交成功',icon:'success'});setTimeout(()=>{wx.navigateBack()},1200)},1200)
  }
})
